<template>
  <div>
    <!-- 1. 面包屑导航 -->
    <!-- <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb> -->

    <!-- 2. 卡片主体 -->
    <el-card>
      <!-- 2.1 搜索 & 添加 -->
      <!-- 搜索栏 -->
	<el-row :gutter="20" style="margin-bottom: 15px">
	<el-col :span="6">
		<el-input
		v-model="queryInfo.username"
		placeholder="请输入用户名"
		clearable
		@clear="getUserList"
		@keyup.enter="getUserList"
		/>
	</el-col>

	<el-col :span="6">
		<el-input
		v-model="queryInfo.phone"
		placeholder="请输入手机号"
		clearable
		@clear="getUserList"
		@keyup.enter="getUserList"
		/>
	</el-col>

	<el-col :span="6">
		<el-button type="primary" icon="Search" @click="getUserList">查询</el-button>
		<el-button @click="resetQuery">重置</el-button>
	</el-col>
	</el-row>

      <!-- 2.2 表格 -->
      <el-table
        :data="tableData"
        border
        stripe
        style="width: 100%; margin-top: 15px"
      >
        <el-table-column type="index" label="#" width="50" />
        <el-table-column prop="username" label="姓名" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column prop="phone" label="电话" />
        <el-table-column prop="roleName" label="角色" />
        <el-table-column label="状态">
          <template #default="{ row }">
            <el-switch v-model="row.mgState" @change="changeState(row)" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleEdit(row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 2.3 分页 -->
      <el-pagination
        v-model:currentPage="queryInfo.pageNum"
        v-model:page-size="queryInfo.pageSize"
        :page-sizes="[5, 10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="getUserList"
        @current-change="getUserList"
        style="margin-top: 15px; text-align: right"
      />
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import API from '../../utils/api'


// 查询参数
const queryInfo = reactive({
  username: '',
  phone: '',
  pageNum: 1,
  pageSize: 10
})
const tableData = ref([])
const total = ref(0)

// 获取列表
const getUserList = async () => {
	let data =await API.getUserList(queryInfo);
	tableData.value = data.records
	total.value = data.total
}

// 状态切换
const changeState = (row) => {
  // 可在此处调用接口持久化状态
  ElMessage.success(`${row.username} 状态已切换为 ${row.mgState}`)
}

// 占位方法
const handleAdd = () => ElMessage.info('点击了添加')
const handleEdit = (row) => ElMessage.info(`编辑 ${row.username}`)
const handleDelete = (row) =>
  ElMessageBox.confirm(`确认删除 ${row.username} 吗？`)
    .then(() => ElMessage.success('已删除'))
    .catch(() => {})

function resetQuery(){
	queryInfo.username='';
	queryInfo.phone='';
	queryInfo.pageNum=1;
	queryInfo.pageSize=10
	getUserList()
}
onMounted(()=>{
	getUserList()

})
</script>

<style scoped>
.breadcrumb {
  margin-bottom: 15px;
}
</style>